package com.castle.fortress.admin.rule.listRule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.rule.listRule.entity.RuleListRuleEntity;
import com.castle.fortress.admin.rule.listRule.dto.RuleListRuleDto;
import com.castle.fortress.admin.rule.listRule.dto.RuleListRuleWithInfoDto;
import com.castle.fortress.admin.rule.listRule.service.RuleListRuleService;
import com.castle.fortress.admin.utils.Sequence;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 规则清单与规则关联表 控制器
 *
 * @author
 * @since 2025-04-22
 */
@Api(tags="规则清单与规则关联表管理控制器")
@Controller
public class RuleListRuleController {
    @Autowired
    private RuleListRuleService ruleListRuleService;

    private List<RuleListRuleDto> ruleList;

    /**
     * 规则清单与规则关联表的分页展示
     * @param ruleListRuleDto 规则清单与规则关联表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("规则清单与规则关联表分页展示")
    @GetMapping("/rule/listRule/ruleListRule/page")
    @ResponseBody
    public RespBody<IPage<RuleListRuleDto>> pageRuleListRule(RuleListRuleDto ruleListRuleDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<RuleListRuleDto> page = new Page(pageIndex, pageSize);
        IPage<RuleListRuleDto> pages = ruleListRuleService.pageRuleListRule(page, ruleListRuleDto);

        return RespBody.data(pages);
    }

    /**
     * 规则清单与规则关联表的列表展示
     * @param ruleListRuleDto 规则清单与规则关联表实体类
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("规则清单与规则关联表列表展示")
    @GetMapping("/rule/listRule/ruleListRule/list")
    @ResponseBody
    public RespBody<List<RuleListRuleDto>> listRuleListRule(RuleListRuleDto ruleListRuleDto){
        List<RuleListRuleDto> list = ruleListRuleService.listRuleListRule(ruleListRuleDto);
        return RespBody.data(list);
    }

    /**
     * 根据合同ID查询规则清单与规则关联及规则详细信息列表
     * @param ruleListRuleDto
     * @return 联合查询结果列表
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("根据合同ID查询规则清单与规则关联及规则详细信息列表")
    @GetMapping("/rule/listRule/ruleListRule/withInfo")
    @ResponseBody
    public RespBody<List<RuleListRuleWithInfoDto>> listRuleListRuleWithInfo(RuleListRuleDto ruleListRuleDto){
        if(ruleListRuleDto == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        List<RuleListRuleWithInfoDto> list = ruleListRuleService.listRuleListRuleWithInfo(ruleListRuleDto);
        return RespBody.data(list);
    }

    /**
     * 规则清单与规则关联表保存
     * @param ruleListRuleDto 规则清单与规则关联表实体类
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("规则清单与规则关联表保存")
    @PostMapping("/rule/listRule/ruleListRule/save")
    @ResponseBody
    public RespBody<String> saveRuleListRule(@RequestBody RuleListRuleDto ruleListRuleDto){
        if(ruleListRuleDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        RuleListRuleEntity ruleListRuleEntity = ConvertUtil.transformObj(ruleListRuleDto,RuleListRuleEntity.class);
        ruleListRuleEntity.setId(Sequence.getInstance().nextId());
        if(ruleListRuleService.save(ruleListRuleEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 规则清单与规则关联表批量保存
     * @param ruleListRuleDtoList 规则清单与规则关联表实体类列表
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("规则清单与规则关联表批量保存")
    @PostMapping("/rule/listRule/ruleListRule/saveBatch")
    @ResponseBody
    public RespBody<String> saveRuleListRuleBatch(@RequestBody List<RuleListRuleDto> ruleListRuleDtoList){
        if(ruleListRuleDtoList == null || ruleListRuleDtoList.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(ruleListRuleService.saveBatch(ruleListRuleDtoList)){
            return RespBody.data("批量保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 规则清单与规则关联表编辑
     * @param ruleListRuleDto 规则清单与规则关联表实体类
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("规则清单与规则关联表编辑")
    @PostMapping("/rule/listRule/ruleListRule/edit")
    @ResponseBody
    public RespBody<String> updateRuleListRule(@RequestBody RuleListRuleDto ruleListRuleDto){
        if(ruleListRuleDto == null || ruleListRuleDto.getId() == null || ruleListRuleDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        RuleListRuleEntity ruleListRuleEntity = ConvertUtil.transformObj(ruleListRuleDto,RuleListRuleEntity.class);
        if(ruleListRuleService.updateById(ruleListRuleEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 规则清单与规则关联表删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("规则清单与规则关联表删除")
    @PostMapping("/rule/listRule/ruleListRule/delete")
    @ResponseBody
    public RespBody<String> deleteRuleListRule(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(ruleListRuleService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 规则清单与规则关联表批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("规则清单与规则关联表批量删除")
    @PostMapping("/rule/listRule/ruleListRule/deleteBatch")
    @ResponseBody
    public RespBody<String> deleteRuleListRuleBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(ruleListRuleService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 规则清单与规则关联表详情
     * @param id 规则清单与规则关联表id
     * @return
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("规则清单与规则关联表详情")
    @GetMapping("/rule/listRule/ruleListRule/info")
    @ResponseBody
    public RespBody<RuleListRuleDto> infoRuleListRule(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        RuleListRuleEntity ruleListRuleEntity = ruleListRuleService.getById(id);
        RuleListRuleDto ruleListRuleDto = ConvertUtil.transformObj(ruleListRuleEntity,RuleListRuleDto.class);

        return RespBody.data(ruleListRuleDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="规则清单与规则关联表",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/rule/listRule/ruleListRule/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<RuleListRuleDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<RuleListRuleDto> list = ruleListRuleService.listRuleListRule(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
